package com.lanhu.designpattern.prototype.sample;

import com.lanhu.designpattern.prototype.sample.WordDocument;

import java.util.ArrayList;
import java.util.List;

/**
 * author:zhangzg
 * date:2020/11/8 17:51
 */
public class Test {
    public static void main(String[] args) {

        int number = 10000000;

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                WordDocument wordDocument = new WordDocument("我是蓝狐", "蓝狐");
                long startTime = System.currentTimeMillis();
                for (int i = 0; i < number; i++) {
                    WordDocument wordDocumentTmp  =  wordDocument.clone();
//                    System.out.println(wordDocumentTmp.toString());

                }
                System.out.println("使用克隆，1000个对象,costTime:" + (System.currentTimeMillis() - startTime));
            }
        });
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                WordDocument wordDocument = new WordDocument("我是蓝狐", "蓝狐");
                long startTime = System.currentTimeMillis();
                for (int i = 0; i < number; i++) {
                    WordDocument wordDocumentTmp  =  wordDocument.clone();
//                    System.out.println(wordDocumentTmp.toString());

                }
                System.out.println("使用new关键字,1000个对象,costTime:" + (System.currentTimeMillis() - startTime));
            }
        });

        thread.start();
        thread1.start();

        List<String> document = new ArrayList<>();
        document.add("test");
        document.add("test2");
        WordDocument wordDocument = new WordDocument("我是蓝狐","蓝狐", document);
        WordDocument clone1 = wordDocument.clone();
        WordDocument clone2 = wordDocument.clone();
        document.add("test2");
        System.out.println(clone1.getDocument().size() + ":" + clone1.getDocument().hashCode());
        System.out.println(clone2.getDocument().size() + ":" + clone2.getDocument().hashCode());

    }
}
